<?php
class NodeAction extends LoginAction
{
    public function index()
    {

    }

    /**
     * 用户角色管理
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function nodes()
    {
        $pNode = $this->getCurrentNode();
        if ($pNode) {
            $this->assign('pNode', $pNode);
        }
        else {
            $this->error('父类节点信息获取错误！');
        }
        if (intval($pNode['level']) > 2) {
            $this->error('最大等级为3级！');
        }
        $pid = intval(getGet('pid'));
        $nodes = D('Node')->getNodes($pid, $pNode['level'] + 1);
        $this->assign('nodes', $nodes);
        $this->display();
    }

    /**
     * 添加节点
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function add()
    {
        $level = 1;
        $pid = intval(getGet('pid'));
        if ($pid) {
            $pNode = $this->getCurrentNode();
            $level = $pNode['level'] + 1;
            $this->assign('pNode', $pNode);
        } else {
            $level = 1;
        }
        if(isPost()){
            $_POST['level'] = $level;
            $_POST['pid'] = $pid;
            $this->createAdd('Node');
        }
        $this->assign('level', $level);
        $this->display();
    }

    /**
     * 修改节点
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function edit()
    {
        $NodeModel = M('Node');
        $id = intval(getGet('id'));
        $node = $NodeModel->find($id);
        if(!$node){
            $this->error('节点修改中接点修改数据获取失败！');
        }
        if(isPost()){
            $this->createUpdate('Node');
            $node = $NodeModel->find($id);
        }
        $this->assign('node',$node);
        $this->display();
    }

    /**
     * 验证当前节点信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    private function getCurrentNode()
    {
        $pid = intval(getGet('pid'));
        if (!$pid) {
            $this->error('节点父类标识不能为0数据信息不对！');
        }
        //获取节点
        $pNode = M('Node')->find($pid);
        if (!$pNode) {
            $this->error('节点信息不对失败！');
        }
        return $pNode;
    }

    private function getNodeRelation($node){
        //判断是否是一级
        return $node;
    }
}

?>